var list = '';
var page = 1;
var pageSize = 10;
var keyWord = '';

//点击搜索
$('.search-btn').on('click',function(){
	 searchEvent();
})

$('.search-input').bind('keypress',function(e){//回车事件 
		if(e.keyCode == "13"){  
				searchEvent();
		}  
}); 

function searchEvent(){//搜索
	var keyWord = $.trim($('.search-input').val());
	initList(page,keyWord,function(res){
		   $('#tableFromData').show();
			$("#tableFromData").bootstrapTable('load', res)
	});
}

function initList(page,keyWord,cb){
	getList(page,keyWord,function(res){
		cb&&cb(res.rows)//更新列表
		// 分页
		$('.M-box11').pagination({
		    totalData:res.total,
		    showData:pageSize,
		    mode: 'fixed',
				count:4,
				pageCount:Math.ceil(res.total/pageSize),
		    callback:function(e){
					var currentPage = e.getCurrent();
					getList(currentPage,keyWord,function(res){
						$("#tableFromData").bootstrapTable('load', res.rows)
					});
		    }
		});
	});
}


//初始化数据处理
function discountList(data) {
   for(var i in data){
      var btnClass = data[i].type==1?"btn-danger":"btn-primary";
	  data[i].payDiscount = data[i].payDiscount<1?data[i].payDiscount:data[i].payDiscount*10/100;
      data[i].action = '<button class="btn btn-primary set-discount" data-index='+i+' data-id='+ data[i].gameUserId +'>设置</button>'
   }
}


//获取充值优惠列表
// function getList(page,keyWord,callback){
// 		var params = {
// 			pageNum:page,
// 			pageSize:pageSize,
// 			nickname:keyWord
// 		};
// 	  _POST('/doulai/discount/list',params).then(function(res){
// 			  list = res.rows;
// 				discountList(list);
// 				InitTable(list);
// 				callback&&callback(res)
// 		})
// }

// getList('');

//获取用户列表
function getList(page,keyWord,callback){
	var params = {
		pageNum:page,
		pageSize:pageSize,
		"roleIds": ['3'],//1-超级管理员 2-管理员 3-代理
        "loginName":keyWord
	};
	_POST('/system/user/list',JSON.stringify(params),true,'POST','json').then(function(res){
		  list = res.rows;
		  discountList(list);
		  InitTable(list);
		  callback&&callback(res)
	})
}

//初始哈列表
function InitTable(data){
  $('#tableFromData').bootstrapTable({
        data: data?data:list
  })
}


//设置充值优惠
$(document).on('click','.set-discount',function () {
	  var id = $(this).data('id');
	  var index = $(this).data('index');
    inputModal('设置充值优惠',function(val){
         discountAction(id,val,function(){
			 closeModal();
			 popup.tips("设置成功",1000);
			 //更新数据
			 list[index].discount = val;
			 discountList(list);
			 $("#tableFromData").bootstrapTable('load', list)
		 })
    })
})

//加减优惠
$(document).on('click','.btn-up',function(){
   //最大不超过0.8
   var discount = $('.discount-val').val();
   if(!(discount<0.8)){

   }else{
     discount = (discount*10 + 0.05*10)/10;
     $(".discount-val").val(discount);
   }
})

$(document).on('click','.btn-down',function(){
   //最小0.05
   var discount = $('.discount-val').val();
   if(discount>0.05){
      discount = (discount*10 - 0.05*10)/10;
   }
     $(".discount-val").val(discount);
})

//设置优惠请求
function discountAction(id,val,cb){
	 var params = "?gameUserId="+id+"&payDiscount="+val;
	 _POST('/doulai/discount/edit'+params,{}).then(function(res){
		  cb&&cb();
	 })
}


function inputModal(title,callback){
   var formHtml = '<div class="set-discount-box">'+
                      '<span class="btn-down">-</span>'+
                      '<input type="text" class="discount-val" name="" value="0.8" disabled >'+
					  '<span class="btn-up">+</span>'+
                  '</div>';
   popup.alert({
     title:title,
     val:formHtml,
     sureCb:function(){
			 var payDiscount = $('.discount-val').val();
			 callback&&callback(payDiscount);
     }
   })
}

function closeModal(){
   popup.closeModal(".popup-closeModal");
}
